package com.hdu.clj.dncard.databaseUtil

import android.content.ContentValues
import android.content.Context
import com.hdu.clj.dncard.database.MonsterDatabase
import com.hdu.clj.dncard.util.Monster

class MonDatabaseUtil {

    fun insert(context: Context,mon: Monster){
        val dbMon = MonsterDatabase(context,"Monster.db",1)
        val dbm = dbMon.writableDatabase
        val values = ContentValues().apply {
            put("mid",mon.mid)
            put("attack",mon.attack)
            put("mname",mon.mname)
            put("life",mon.life)
        }
        dbm.insert("Monster",null,values)
    }
    fun search(context: Context,mid:String):Array<String>{
        val dbMon = MonsterDatabase(context,"Monster.db",1)
        val dbm = dbMon.writableDatabase
        val find = dbm.rawQuery("select * from Monster where mid = ?", arrayOf(mid))
        var mname = ""
        var mattack = 0
        var mlife = 0
        if (find.moveToFirst()){
            mname = find.getString(find.getColumnIndex("mname"))
            mattack = find.getInt(find.getColumnIndex("attack"))
            mlife = find.getInt(find.getColumnIndex("life"))
        }
        return arrayOf(mname,mattack.toString(),mlife.toString())
    }
}